package arm.attestation.bean;

import arm.attestation.entity.Contract;
import arm.dic.entity.DicOrganization;
import arm.common.manager.TableManager;
import arm.db.tools.DBManager;

import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.List;

public class ContractBean
{

    private Contract contract;
    private TableManager<Contract> tableManager;
    private DicOrganization selectedOrg;

    public Contract getContract() {
        return contract;
    }

    public void setContract(Contract contract) {
        this.contract = contract;
    }

    public String viewTable ()
    {
        getTableManager().readFromDB();
        return "viewContractTable" ;
    }

    public TableManager<Contract> getTableManager () {
        if (tableManager==null)
        {
            tableManager=new TableManager<Contract>(Contract.class);
        }
        return tableManager;
    }

    public void setTableManager (TableManager<Contract> tableManager) {
        this.tableManager=tableManager;
    }

    public String create()
    {
        contract = new Contract();
        return "editContract";
    }

    public String save()
    {
        DBManager.saveOrUpdate(getContract());
        return viewTable();
    }

    public String edit () {
        setContract(getTableManager().getSelectedObj());
        return "editContract";
    }

    public  String cancel () {
        return viewTable();
    }

    public void delete () {
        DBManager.delete(getTableManager().getSelectedObj());
        getTableManager().readFromDB();
    }

    public List<SelectItem> getOrgList()
    {
        List<DicOrganization> objs = DBManager.readList(DicOrganization.class.getName());
        List<SelectItem> selectItems = new ArrayList();
        for (DicOrganization obj : objs)
        {
            selectItems.add(new SelectItem(obj, obj.getName()));
        }
        return selectItems;
    }
}